package com.foreveross.service;

import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.hibernate.ejb.criteria.expression.function.AggregationFunction.COUNT;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.common.Constants;
import com.foreveross.dao.SysMenuManagerDAO;
import com.foreveross.dao.SysRoleMenuManagerDAO;
import com.foreveross.vo.HrHouses;
import com.foreveross.vo.SysMenu;

@Service("sysMenuManagerService")
@Transactional
public class SysMenuManagerService {

	@Resource
	SysMenuManagerDAO sysMenuManagerDAO;
	@Resource
	SysRoleMenuManagerDAO sysRoleMenuManagerDAO;

	public <T> void saveOrUpdate(T entity, String type, String id)
			throws Exception {
		if (type.equals(Constants.UPDATE_ADD)) {
			sysMenuManagerDAO.save(entity);
		} else if (type.equals(Constants.UPDATE_EDIT)) {
			sysMenuManagerDAO.updateObject(entity, id);
		}

	}

	public Object getObjectById(Class clazz, String id) throws Exception {
		return sysMenuManagerDAO.findById(clazz, id);
	}

	public Object getObjectByIntId(Class clazz, int id) throws Exception {
		return sysMenuManagerDAO.findByIntId(clazz, id);
	}

	public List<SysMenu> getSysMenuList(String name, int pageIndex, int pageSize)
			throws Exception {
		return sysMenuManagerDAO.findSysMenuList(name, pageIndex, pageSize);
	}

	public long getSysMenuListCount(String name) throws Exception {
		return sysMenuManagerDAO.findSysMenuListCount(name);
	}

	public void deleteSysMenu(String ids) throws Exception {
		for (String id : ids.split(",")) {
			sysRoleMenuManagerDAO.deleteSysRoleMenuByMenuId(id);
			sysMenuManagerDAO.deleteSysMenu(id);
		}
	}

	public List<SysMenu> getSysRoleMenu(String roleId, String updateType)
			throws Exception {
		List<SysMenu> sysRoleMenuList = sysMenuManagerDAO
				.findSysMenuList(roleId);
		List<SysMenu> sysMenuList = sysMenuManagerDAO.findSysMenuList();
		for (SysMenu sysMenu : sysMenuList) {
			if (updateType.equals(Constants.UPDATE_WATCH))
				sysMenu.setChkDisabled(true);
			sysMenu.setOpen(true);
			for (SysMenu sysRoleMenu : sysRoleMenuList) {
				if (sysMenu.getId().equals(sysRoleMenu.getId()))
					sysMenu.setChecked(true);
			}
		}
		return sysMenuList;
	}

	public JSONArray getJSONArray(List<SysMenu> menuList, int start, int level) {
		JSONArray list = new JSONArray();
		for (int i = start; i < menuList.size(); i++) {
			SysMenu sysMenu = menuList.get(i);
			System.out.println(sysMenu.getName());
			int currentLevel = (sysMenu.getId().length() - 3) / 2 + 1;
			if (currentLevel != level || currentLevel == 1) {
				break;
			}
			JSONObject jobj = new JSONObject();
			jobj.put("id", sysMenu.getId());
			jobj.put("text", sysMenu.getName());
			jobj.put("url", sysMenu.getUrl());
			if (i + 1 < menuList.size()) {
				SysMenu sysMenuNext = menuList.get(i + 1);
				if (sysMenu.getId().equals(sysMenuNext.getParentid())) {
					JSONArray c = getJSONArray(menuList, i + 1,
							currentLevel + 1);
					jobj.put("children", c);
					jobj.put("state", "closed");
					i += c.size();
					if (level > 1)
						level -= 1;
				}
			}
			list.add(jobj);

		}
		return list;
	}

	public JSONArray findroleHrHouse(String roleid) throws Exception {
		List houseList = sysMenuManagerDAO.findroleHrHouse(roleid);
		JSONArray list = new JSONArray();
		for (int i = 0; i < houseList.size(); i++) {
			JSONObject obj = new JSONObject();
			Object[] house = (Object[]) houseList.get(i);
			obj.put("id", house[0]);
			obj.put("text", house[1]);
			if (house[2] != null && !"".equals(house[2].toString()))
				obj.put("checked", true);
			list.add(obj);
		}
		return list;

	}

	public JSONArray findRoleMenuList(String pid, String roleid)
			throws Exception {
		JSONArray list = new JSONArray();
		List<Object[]> menuList = sysMenuManagerDAO.findRoleMenuList(pid,
				roleid);
		for (int i = 0; i < menuList.size(); i++) {
			JSONObject obj = new JSONObject();
			Object[] sysMenu = (Object[]) menuList.get(i);
			obj.put("id", sysMenu[0]);
			obj.put("text", sysMenu[1]);
			obj.put("url", sysMenu[2]);
			if (sysMenu[3] != null && !"".equals(sysMenu[3].toString()))
				obj.put("checked", true);
			List<Object[]> menuListv = sysMenuManagerDAO.findRoleMenuList(
					sysMenu[0].toString(), roleid);
			if (menuListv != null && !menuListv.isEmpty()) {
				obj.put("children",
						findRoleMenuList(sysMenu[0].toString(), roleid));
				// obj.put("state", "closed");
			}
			list.add(obj);
		}

		return list;
	}

	public JSONArray fatherMenuList(String pid, String menuId) throws Exception {
		JSONArray list = new JSONArray();
		List<SysMenu> menuList = sysMenuManagerDAO.findSysMenuList(pid, null);
		for (SysMenu sysMenu : menuList) {
			JSONObject obj = new JSONObject();
			obj.put("id", sysMenu.getId());
			obj.put("text", sysMenu.getName());
			obj.put("url", sysMenu.getUrl());
			if (menuId != null && sysMenu.getUrl() != null
					&& menuId.equals(sysMenu.getId()))
				obj.put("checked", true);
			List<SysMenu> menuListv = sysMenuManagerDAO.findSysMenuList(
					sysMenu.getId(), null);
			if (menuListv != null && !menuListv.isEmpty()) {
				obj.put("children",
						fatherMenuList(sysMenu.getId(), menuId));
				// obj.put("state", "closed");
			}
			list.add(obj);
		}

		return list;
	}

	public JSONArray getMenuList(String pid, String roleid) throws Exception {
		JSONArray list = new JSONArray();
		List<SysMenu> menuList = sysMenuManagerDAO
				.findSysMenuList(pid, roleid);
		for (SysMenu sysMenu : menuList) {
			JSONObject obj = new JSONObject();
			obj.put("id", sysMenu.getId());
			obj.put("text", sysMenu.getName());
			obj.put("url", sysMenu.getUrl());
			List<SysMenu> menuListv = sysMenuManagerDAO.findSysMenuList(
					sysMenu.getId(), roleid);
			if (menuListv != null && !menuListv.isEmpty()) {
				obj.put("children", getMenuList(sysMenu.getId(), roleid));
				obj.put("state", "closed");
			}
			list.add(obj);
		}

		return list;
	}

}
